/*
 * Copyright 2013 Wajdi Hh "wajdihh@gmail.com" .
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hh.ui.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.hh.droid.R;
import com.hh.listeners.OnHideBarListener;
import com.hh.listeners.OnNavItemsClickListener;
import com.hh.listeners.OnShowBarListener;

/**
 * 
 * A Vertical navigation bar with scroll feature and custom item 
 * 
 * @author WajdiHh : Last modification : 23/05/2013
 *
 */

public class UiVerticalNavBar extends LinearLayout implements OnClickListener, OnItemClickListener {


	private View mConvertViewV;
	private Button _mBtnRight;
	private Button _mBtnLeft;
	private RelativeLayout _mNavBarV;
	private FrameLayout _mFlBtnRight;
	private FrameLayout _mFlBtnLeft;
	private ListView _mVListView;
	private OnNavItemsClickListener _mListener;
	private FrameLayout _mCLickedZone;
	private OnShowBarListener _mShowListener;
	private OnHideBarListener _mHideListener;
	private boolean _mIsShowing;

	/**
	 * A Vertical navigation bar with scroll feature and custom item 
	 * @param context
	 * @param attrs
	 */
	public UiVerticalNavBar(final Context context, AttributeSet attrs) {

		super(context, attrs);

		mConvertViewV = LayoutInflater.from(context).inflate(R.layout.ui_vnavigation_barre, this);

		_mBtnRight = (Button) mConvertViewV.findViewById(R.id.NavBar_V_BtnRight);
		_mBtnRight.setOnClickListener(this);

		_mBtnLeft = (Button) mConvertViewV.findViewById(R.id.NavBar_V_BtnLeft);
		_mBtnLeft.setOnClickListener(this);

		_mNavBarV = (RelativeLayout) mConvertViewV.findViewById(R.id.NavBar_V_SubContainer);

		_mFlBtnRight=(FrameLayout) findViewById(R.id.NavBar_V_FLBtnRight);
		_mFlBtnLeft=(FrameLayout) findViewById(R.id.NavBar_V_FLBtnLeft);

		_mVListView=(ListView) findViewById(R.id.NavBar_V_HListView);	

		_mVListView.setOnItemClickListener(this);

		_mCLickedZone=(FrameLayout) findViewById(R.id.clickedZoneV);
		_mCLickedZone.setOnClickListener(this);

		/*
		 * Default color
		 */
		int lDefaultColor=getResources().getColor(R.color.holo_gray_lev0);

		_mVListView.setBackgroundColor(lDefaultColor);

		_mFlBtnRight.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_round_vert_navbar));
		_mFlBtnLeft.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_round_vert_navbar));

		_mIsShowing=false;
	}

	/**
	 * <br>
	 * Set the background of the right button (button which open the view)
	 * 
	 * @author WajdiHh
	 * @param pDrawable :icon to set
	 */
	public void setBackgroundButtonRight(Drawable pDrawable){	
		_mFlBtnRight.setBackgroundDrawable(pDrawable);
		_mBtnRight.setBackgroundDrawable(null);
	}
	
	/**
	 * <br>
	 * Set the background of the bottom left (button which close the view)
	 * 
	 * @author WajdiHh
	 * @param pDrawable :icon to set
	 */
	
	public void setBackgroundButtonLeft(Drawable pDrawable){	
		_mFlBtnLeft.setBackgroundDrawable(pDrawable);
		_mBtnLeft.setBackgroundDrawable(null);
	}
	/**
	 * <br>
	 * Set the background color of the navigation bar
	 * 
	 * @author WajdiHh
	 * @param pColor: icon to set
	 */	
	public void setBackgroundNavBar(int pColor){
		_mVListView.setBackgroundColor(pColor);

	}

	public void setWidth(int pWidth){

		RelativeLayout.LayoutParams params = 
				new RelativeLayout.LayoutParams(pWidth, RelativeLayout.LayoutParams.WRAP_CONTENT);

		_mVListView.setLayoutParams(params);
	}

	public void setHeight(int pHeight){

		RelativeLayout.LayoutParams params = 
				new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,pHeight);

		_mVListView.setLayoutParams(params);
	}
	/**
	 * Specify an alpha value for the drawable button right. 0 means fully transparent, and 255 means fully opaque. 

	 * @param pAlpha
	 */
	public void setAlphaButtonRight(int pAlpha){
		_mFlBtnRight.getBackground().setAlpha(pAlpha);
	}

	/**
	 * Specify an alpha value for the drawable of the navigation bar. 0 means fully transparent, and 255 means fully opaque. 

	 * @param pAlpha
	 */
	public void setAlphaNavBar(int pAlpha){
		_mVListView.getBackground().setAlpha(pAlpha);
		_mFlBtnLeft.getBackground().setAlpha(pAlpha);
	}

	/**
	 * <br>
	 * Specify the adapter to display data
	 * 
	 * @author WajdiHh
	 * @param pAdapter : Adapter
	 */
	public void setAdapter(BaseAdapter pAdapter){

		if(pAdapter!=null){
			_mVListView.setAdapter(pAdapter);
		}
	}

	/**
	 * <br>
	 * Check if navigation bar is Showing
	 * 
	 * @author WajdiHh
	 * @return  true : showing / false hiding
	 */
	public boolean isShowing(){
		return _mIsShowing;
	}	
	/**
	 * <br>
	 * Hide the navigation bar
	 *  @author WajdiHh
	 */
	
	public void hideBar(){

		Animation lAnimationHide=AnimationUtils.loadAnimation(getContext(),R.anim.push_in_left);
		_mNavBarV.startAnimation(lAnimationHide);	
		lAnimationHide.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				_mNavBarV.setVisibility(View.INVISIBLE);
			}	
			@Override
			public void onAnimationRepeat(Animation animation) {}

			@Override
			public void onAnimationEnd(Animation animation) {
				_mFlBtnRight.setVisibility(View.VISIBLE);	
				_mFlBtnRight.startAnimation(AnimationUtils.loadAnimation(getContext(),R.anim.fadein));
				_mNavBarV.setVisibility(View.GONE);				
			}
		});

		if(_mHideListener!=null)
			_mHideListener.onHideBar();
		
		_mIsShowing=false;
	}

	/**
	 * <br>
	 * show the navigation bar
	 *  @author WajdiHh
	 */
	
	public void showBar(){

		_mNavBarV.setVisibility(View.VISIBLE);
		_mFlBtnRight.setVisibility(View.GONE);
		_mNavBarV.startAnimation(AnimationUtils.loadAnimation(getContext(),R.anim.push_in_right));

		if(_mShowListener!=null)
			_mShowListener.onShowBar();
		
		_mIsShowing=true;
	}
	/**
	 * <br>
	 * Register a callback to be invoked when navigation bar is opened
	 * 
	 * @author WajdiHh
	 * @param pListener
	 * @see OnShowBarListener
	 */
	public void setOnShowBarListener(OnShowBarListener pListener){
		_mShowListener=pListener;
	}
	/**
	 * <br>
	 * Register a callback to be invoked when navigation bar is closed
	 * 
	 * @author WajdiHh
	 * @param pListener
	 * @see OnHideBarListener
	 */
	public void setOnHideBarListener(OnHideBarListener pListener){
		_mHideListener=pListener;
	}
	/**
	 * <br>
	 * Register a callback to be invoked when navigation bar item is clicked
	 * 
	 * @author WajdiHh
	 * @param pListener
	 * @see OnNavItemsClickListener
	 */
	public void setOnNavItemsClickListener(OnNavItemsClickListener pListener){
		_mListener=pListener;
	}
	
	@Override
	public void onClick(View v) {

		if(v.getId()==R.id.NavBar_V_BtnRight){

			showBar();

		}else if(v.getId()==R.id.NavBar_V_BtnLeft||v.getId()==R.id.clickedZoneV ){

			hideBar();
		}
	}



	//TODO 
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		System.out.println("yyyyyyyyyyyyyy");
		if(_mListener!=null)
			_mListener.onNavItemClick(arg0,arg1, arg2, arg3);
	}
}
